﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace PIM_ATE.Electrical
{
    partial class FrmSConfigNewAntenna : Form
    {
        public FrmSConfigNewAntenna(ClsDataSParamBuilder configBuilder)
        {
            InitializeComponent();
            m_cfgBuilder = configBuilder;
        }

        ClsDataSParamBuilder m_cfgBuilder;
        List<clsTestItem> m_lstConfig;
        DataTable m_dtConfig;
        int m_switch_port_count = 8;
        Dictionary<string, TestPort> m_dicPort = new Dictionary<string, TestPort>();

        class TestPort
        {
            public Dictionary<string, TestBand> dicBand = new Dictionary<string, TestBand>();
        }
        class TestAgl
        {
            public string agl;
            public TDSpecParam param;
        }
        class TestBand
        {
            public string band;
            public string mark;
            public Dictionary<string, TestAgl> dicAgl = new Dictionary<string, TestAgl>();
        }
        class Param
        {
            public bool bEnable_InsertlossSpec;
            public string sInsertlossSpec;
            public bool bEnable_MagSpec;
            public string sMagSpec;
            public bool bEnable_IsoSpec_SameFreq;
            public string sIsoSpec_SameFreq;
            public bool bEnable_IsoSpec_Freq;
            public string sIsoSpec_Freq;
            public bool bEnable_IsoSpec_Port;
            public string sIsoSpec_Port;
            public bool bEnable_Vswr;
            public string sVswr;
        }

        List<string> lstBand = new List<string>();
        List<string> lstRCU = new List<string>();
        List<string> lstAngle_max = new List<string>();
        List<string> lstAngle_min = new List<string>();
        List<Param> lstParam = new List<Param>();
        List<bool> lstcheckres = new List<bool>();
        List<int> lstretspeed = new List<int>();
        List<string> lstRcuTip = new List<string>();

        string[] csnTestType = new string[5] { "驻波", "驻波", "隔离", "插损", "耦合" };
        string[] csnPortType = new string[5] { "S11", "S22", "S12", "S21", "S21" };
        string[] csnFormatType = new string[5] { "DISP_SWR", "DISP_SWR", "DISP_LOGM", "DISP_LOGM", "DISP_LOGM" };


        class TDSpecParam : Param
        {
            public bool bEnable_MagSpec;
            public string sMagSpec;
            public bool bEnable_AmSpec;
            public string sAmSpec;
            public bool bEnble_PhaseOffestSpec;
            public string sPhaseOffestSpec;
            public bool bEnable_PhaseSpec;
            public string sPhaseSpec;
            public bool bEnable_CoIsoSpec;
            public string sCoIsoSpec;
            public bool bEnable_DeIsoSpec;
            public string sDeIsoSpec;
            public bool bEnable_IsoSpec;
            public string sIsoSpec;
        }

        class TDParams
        {
            public List<AntennaType> lstAntennaType = new List<AntennaType>();
            public List<AntennaTestMethod> lstAntennaTestMethod = new List<AntennaTestMethod>();
            public List<AntennaTestType> lstAntennaTestType = new List<AntennaTestType>();
            public List<string> lstPortCount = new List<string>();
            public List<string> lstBand = new List<string>();
            public List<string> lstRCU = new List<string>();
            public List<string> lstAngle = new List<string>();
            public List<string> lstMarker = new List<string>();
            public List<TDSpecParam> lstParam = new List<TDSpecParam>();
            public List<bool> lstcheckres = new List<bool>();
            public List<int> lstretspeed = new List<int>();
            public string[] csnTestType = new string[4] { "驻波", "驻波", "隔离", "隔离" };
            public string[] csnPortType = new string[4] { "S11", "S22", "S12", "S21" };
            public string[] csnFormatType = new string[5] { "DISP_SWR", "DISP_SWR", "DISP_LOGM", "DISP_LOGM", "DISP_PHAS" };
            public string[] csnTestPort = new string[9] { "P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9" };
        }
        TDParams m_tdparams = new TDParams();

        private void FrmSConfigNewAntenna_Load(object sender, EventArgs e)
        {
            //tabControl1.TabPages.Remove(tabPage2);
            rbMixAntenna.Visible = false;
            //  checkMagSpec.Enabled = checkInsertlossSpec.Enabled = false;

            dvgConfig.ReadOnly = true;
            dvgConfig.RowHeadersVisible = false;
            dvgConfig.AllowUserToAddRows = false;
            dvgConfig.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dvgConfig.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dvgConfig.MultiSelect = false;
            checkManualRCU.Visible = false;
            checkRCUCheck.Visible = false;
            cbRCUType.SelectedIndex = 1;
            retspeed.SelectedIndex = 2;
            retspeed_TD.SelectedIndex = 2; retspeed_TD.Visible = false; checkresult_TD.Visible = false;
            cbTestMethod.SelectedIndex = 1;


            //cbBand.SelectedIndex = 0;
            m_lstConfig = new List<clsTestItem>();

            if (!GDefine.IS_LOCAL_CONFIG_ANTENNA)
            {
                var resultObj = new ClsDataResultForAntennaBuilder(GDefine.DBNameELEC);
                string[] lstName = resultObj.getProcodeListFromTMO();
                if (lstName != null)
                {
                    cbProcode.Items.AddRange(lstName);
                }
                cbProcode.Visible = true;
                textProcode.Visible = false;
                btnSaveLocal.Visible = false;
                btnOK.Visible = true;
            }
            else
            {
                cbProcode.Visible = false;
                textProcode.Visible = true;
                btnSaveLocal.Visible = true;
                btnOK.Visible = false;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            textRcuIndex.Text = textRcuIndex.Text.Replace(" ", "");
            if (textRcuIndex.Text == "")
            {
                MessageBox.Show("不允许空和空格，请重新输入!");
                return;
            }

            //if (lstRCU.IndexOf(textRcuIndex.Text) != -1)
            //{
            //    MessageBox.Show("RCU重复！");
            //    return;
            //}

            string[] sAngle = textAngle.Text.Split(',');
            if (sAngle == null || sAngle.Length == 0 || sAngle.Length > 2)
            {
                MessageBox.Show("角度设置错误(数量不得大于2)!");
                return;
            }
            if (sAngle.Length == 1)
            {
                if (cbRCUType.SelectedIndex != 0)
                {
                    MessageBox.Show("固定角度请设置手动调度!");
                    return;
                }
                lstAngle_max.Add(textAngle.Text);
                lstAngle_min.Add(textAngle.Text);
            }
            else if (sAngle[0] == "" || sAngle[1] == "")
            {
                MessageBox.Show("角度设置错误!");
                return;
            }
            else
            {
                lstAngle_max.Add(sAngle[1]);
                lstAngle_min.Add(sAngle[0]);
            }

            lstBand.Add(textTestBand.Text);
            lstRCU.Add(textRcuIndex.Text);
            lstcheckres.Add(checkresult.Checked);
            lstretspeed.Add(retspeed.SelectedIndex);

            lstRcuTip.Add(textRcuTip1.Text + "&" + textRcuTip2.Text);


            Param p = new Param();

            p.bEnable_InsertlossSpec = checkInsertlossSpec.Checked;
            p.sInsertlossSpec = tbInsertloss.Text;

            p.bEnable_MagSpec = checkMagSpec.Checked;
            p.sMagSpec = tbMagSpec.Text;

            p.bEnable_IsoSpec_Port = checkIsoSpec_Port.Checked;
            p.sIsoSpec_Port = tbIsoSpec_Port.Text;

            p.bEnable_IsoSpec_Freq = checkIsoSpec_Freq.Checked;
            p.sIsoSpec_Freq = tbIsoSpec_Freq.Text;

            p.bEnable_IsoSpec_SameFreq = checkIsoSpec_SameFreq.Checked;
            p.sIsoSpec_SameFreq = tbIsoSpec_SameFreq.Text;

            p.bEnable_Vswr = checkVSWRSpec.Checked;
            p.sVswr = tbVSWRSpec.Text;

            lstParam.Add(p);

            string info = textTestBand.Text + "/" + textRcuIndex.Text + "/" + textAngle.Text + "/" +
                          tbIsoSpec_SameFreq.Text + "/" + tbIsoSpec_Freq.Text + "/" + tbIsoSpec_Port.Text + "/" +
                          tbVSWRSpec.Text + "/" + tbMagSpec.Text + "/" + tbInsertloss.Text;
            listAntennaPort.Items.Add(info);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (rbNormalAntenna.Checked)
            {
                NumalTestConfigBuild();
            }
            else if (rbTdAntenna.Checked)
            {
                button2_Click(sender, e);
            }
            else
            {

            }

            #region
            //if (m_dtConfig != null)
            //    m_dtConfig.Clear();
            //if (m_lstConfig != null)
            //    m_lstConfig.Clear();

            //int TestCount = lstBand.Count * 2 / m_switch_port_count + 1;

            //#region 驻波, 带内隔离
            //for (int j = 0; j < lstBand.Count; j++)
            //{//驻波，带内端口隔离
            //    //string[] csnAngle = new string[2] { "0", lstAngle[j] };
            //    //for (int aglNum = 0; aglNum < 2; aglNum++ )
            //    for (int typeNum = 0; typeNum < csnPortType.Length; typeNum++)
            //    {
            //        var aConfig = new clsTestItem();
            //        aConfig.m_arrDbHead = null;
            //        aConfig.m_arrExcelHead = null;
            //        aConfig.m_arrRCUHead = null;

            //        int test_index = j * 2 / m_switch_port_count;
            //        aConfig.sIndex = test_index.ToString(); // 序号
            //        aConfig.iTestIndex = 0; //接线序号

            //        aConfig.sTestItem = csnTestType[typeNum];
            //        aConfig.sTestName = lstBand[j] + "-" + lstRCU[j] + "/" +
            //                            lstAngle_min[j] + "-" + lstAngle_max[j] + "/" +
            //                            csnTestType[typeNum]; // 参数名称

            //        string sSPec = csnTestType[typeNum] == "驻波" ? lstParam[j].sVswr : lstParam[j].sIsoSpec_Port;
            //        aConfig.sSpec = sSPec; // "客户要求"
            //        aConfig.sFactSpec = sSPec; // 内控

            //        aConfig.sInstrCtrl = ""; // 仪表控制列
            //        aConfig.sFreq = lstBand[j]; // 频率
            //        aConfig.sBand = textOffsetBand.Text; // 校准频段
            //        aConfig.sRandom = "1"; //测试次数 随机范围//1,2, 50~80

            //        if (csnTestType[typeNum] == "驻波" && !lstParam[j].bEnable_Vswr)
            //        {
            //            continue;
            //        }
            //        else if (csnTestType[typeNum] == "隔离" && !lstParam[j].bEnable_IsoSpec_Port)
            //        {
            //            continue;

            //        }

            //        aConfig.sTestFlag = "True";

            //        //aConfig.sPortA = (2 * j + 1).ToString(); // 1端口连接
            //        //aConfig.sPortB = (2 * j + 1 + 1).ToString(); // 2端口连接
            //        aConfig.sPortA = (2 * j % m_switch_port_count + 1).ToString(); // 1端口连接
            //        aConfig.sPortB = (2 * j % m_switch_port_count + 1 + 1).ToString(); // 2端口连接

            //        aConfig.sTestPortType = csnPortType[typeNum]; // 测试类型//S11,S21
            //        aConfig.sFormatType = csnFormatType[typeNum]; // 格式化类型//DISP_LOGM
            //        aConfig.sReport = "最大值的绝对值"; // 报表内容              

            //        aConfig.sPortA_Angle = lstAngle_min[j] + "-" + lstAngle_max[j];
            //        aConfig.sPortB_Angle = lstAngle_min[j] + "-" + lstAngle_max[j];

            //        aConfig.sPortA_RCU = lstRCU[j] + "+45度";
            //        aConfig.sPortB_RCU = lstRCU[j] + "-45度";

            //        aConfig.sTestFunction = "Continue";
            //        aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();
            //        //aConfig.sRCUFlag = !checkManualRCU.Checked ? "1" : "0";

            //        aConfig.sCheckResult = lstcheckres[j];
            //        aConfig.sRetSpeed = lstretspeed[j];
            //        AddTestItem(m_lstConfig, aConfig);


            //    }
            //}
            //#endregion

            //#region 带间隔离
            ////int iMaxAngleNum
            //string[] sPortType = new string[2] { "S12", "S21" };
            //string[] sAntennaPort = new string[2] { "+45度", "-45度" };

            ////int test_band_count = lstBand.Count;
            //for (int test_index = 0; test_index < TestCount; test_index++)
            //{//获取接线次数
            //    int test_port_count = (test_index + 1) * m_switch_port_count;
            //    int test_Port_local = test_index * m_switch_port_count;

            //    //获取每次接线时对应的端口数
            //    if ((test_Port_local + m_switch_port_count + 1) > lstBand.Count * 2)
            //        test_port_count = lstBand.Count * 2;

            //    for (int typeNum = 0; typeNum < sPortType.Length; typeNum++)
            //        for (int angleNum = 0; angleNum < 2; angleNum++)
            //            for (int portA_Num = test_Port_local / 2; portA_Num < test_port_count / 2; portA_Num++)
            //            //for (int portA_Num = 0; portA_Num < lstBand.Count; portA_Num++)
            //            {//端口A
            //                for (int portA_AntNum = 0; portA_AntNum < sAntennaPort.Length; portA_AntNum++)
            //                {//端口A天线端口
            //                    for (int portB_Num = portA_Num + 1; portB_Num < test_port_count / 2; portB_Num++)
            //                    //for (int portB_Num = portA_Num + 1; portB_Num < lstRCU.Count; portB_Num++)
            //                    {//端口B
            //                        for (int portB_AntNum = 0; portB_AntNum < sAntennaPort.Length; portB_AntNum++)
            //                        {//端口B天线端口
            //                            if (lstBand[portA_Num] == lstBand[portB_Num] && sPortType[typeNum] == "S21")
            //                            {//同频带间隔离的S21跳过
            //                                continue;
            //                            }

            //                            var aConfig = new clsTestItem();
            //                            aConfig.m_arrDbHead = null;
            //                            aConfig.m_arrExcelHead = null;
            //                            aConfig.m_arrRCUHead = null;

            //                            aConfig.sIndex = test_index.ToString(); // 序号
            //                            aConfig.iTestIndex = 0; //接线序号

            //                            string sSpec = "";
            //                            if (lstBand[portA_Num] == lstBand[portB_Num])
            //                            {
            //                                aConfig.sTestItem = "同频带间隔离";
            //                                if (sPortType[typeNum] == "S21")
            //                                    sSpec = lstParam[portB_Num].sIsoSpec_SameFreq;
            //                                else
            //                                    sSpec = lstParam[portA_Num].sIsoSpec_SameFreq;

            //                                if (!lstParam[portA_Num].bEnable_IsoSpec_SameFreq)
            //                                    continue;
            //                                //    aConfig.sTestFlag = "True"; //true X
            //                                //else
            //                                //    aConfig.sTestFlag = "False"; //true X
            //                            }
            //                            else
            //                            {
            //                                aConfig.sTestItem = "异频带间隔离";
            //                                if (sPortType[typeNum] == "S21")
            //                                    sSpec = lstParam[portB_Num].sIsoSpec_Freq;
            //                                else
            //                                    sSpec = lstParam[portA_Num].sIsoSpec_Freq;

            //                                if (!lstParam[portA_Num].bEnable_IsoSpec_Freq)
            //                                    continue;
            //                                //    aConfig.sTestFlag = "True"; //true X
            //                                //else
            //                                //    aConfig.sTestFlag = "False"; //true X
            //                            }

            //                            aConfig.sSpec = sSpec; // "客户要求"
            //                            aConfig.sFactSpec = sSpec; // 内控

            //                            aConfig.sInstrCtrl = ""; // 仪表控制列
            //                            aConfig.sFreq = typeNum == 0 ? lstBand[portA_Num] : lstBand[portB_Num]; // 频率
            //                            aConfig.sBand = textOffsetBand.Text; // 校准频段
            //                            aConfig.sRandom = "1"; //(换线)测试次数 随机范围//1,2, 50~80
            //                            aConfig.sTestFlag = "True"; //true X

            //                            //aConfig.sPortA = (2 * portA_Num + portA_AntNum + 1).ToString(); // 1端口连接
            //                            //aConfig.sPortB = (2 * portB_Num + portB_AntNum + 1).ToString(); // 2端口连接
            //                            aConfig.sPortA = (2 * portA_Num % m_switch_port_count + portA_AntNum + 1).ToString(); // 1端口连接
            //                            aConfig.sPortB = (2 * portB_Num % m_switch_port_count + portB_AntNum + 1).ToString(); // 2端口连接

            //                            aConfig.sTestPortType = sPortType[typeNum]; // 测试类型//S11,S21
            //                            aConfig.sFormatType = "DISP_LOGM"; // 格式化类型//DISP_LOGM
            //                            aConfig.sReport = "最大值的绝对值"; // 报表内容              

            //                            string angleA = angleNum == 0 ? lstAngle_min[portA_Num] : lstAngle_max[portA_Num];
            //                            string angleB = angleNum == 0 ? lstAngle_min[portB_Num] : lstAngle_max[portB_Num];

            //                            if (lstAngle_min[portA_Num] == lstAngle_max[portA_Num] &&
            //                                lstAngle_min[portB_Num] == lstAngle_max[portB_Num] &&
            //                                angleNum == 1)
            //                            {
            //                                continue;
            //                            }

            //                            aConfig.sPortA_Angle = angleA;
            //                            aConfig.sPortB_Angle = angleB;
            //                            aConfig.sPortA_RCU = lstRCU[portA_Num] + sAntennaPort[portA_AntNum];
            //                            aConfig.sPortB_RCU = lstRCU[portB_Num] + sAntennaPort[portB_AntNum];
            //                            aConfig.sTestFunction = "Single";
            //                            aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();
            //                            //aConfig.sRCUFlag = !checkManualRCU.Checked ? "1" : "0";

            //                            aConfig.sTestName = lstBand[portA_Num] + "-" + lstRCU[portA_Num] + "/" +
            //                                                lstBand[portB_Num] + "-" + lstRCU[portB_Num] + "/" +
            //                                                aConfig.sPortA_Angle + "/" + aConfig.sPortB_Angle + "/" +
            //                                                aConfig.sTestItem; // 参数名称

            //                            AddTestItem(m_lstConfig, aConfig);
            //                        }
            //                    }
            //                }
            //            }
            //}
            //#endregion

            //m_dtConfig = ClsDataSParamBuilder.toTable(m_lstConfig);

            //dvgConfig.DataSource = m_dtConfig;
            //if (dvgConfig.Columns[0].HeaderText != "")
            //{
            //    var colCheck = new DataGridViewCheckBoxColumn();
            //    dvgConfig.Columns.Insert(0, colCheck);
            //}
            //for (int i = 0; i < dvgConfig.Rows.Count; i++)
            //{
            //    dvgConfig.Rows[i].Cells[0].Value = true;
            //}
            #endregion
        }

        private void btnSaveLocal_Click(object sender, EventArgs e)
        {
            if (m_lstConfig == null || m_lstConfig.Count == 0)
            {
                MessageBox.Show("请生成预览");
                return;
            }

            if (textProcode.Text == "")
            {
                MessageBox.Show("请输入产品名");
                return;
            }

            if (ClsDataSParamBuilder.SerializeFile(Application.StartupPath + "\\config\\" + textProcode.Text + GDefine.SUFFIX_STRING_SPARAM,
                                                        getFinalyConfig().ToArray()))
            {
                MessageBox.Show("保存本地成功!");
            }
            else
            {
                MessageBox.Show("保存本地失败!");
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (m_dtConfig != null)
                m_dtConfig.Clear();
            if (m_lstConfig != null)
                m_lstConfig.Clear();

            listAntennaPort.Items.Clear();
            lstAngle_max.Clear();
            lstAngle_min.Clear();
            lstBand.Clear();
            lstParam.Clear();
            lstRCU.Clear();
            lstRcuTip.Clear();

            m_tdparams = new TDParams();
            lstParam = new List<Param>();

            m_dicPort.Clear();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (m_lstConfig == null || m_lstConfig.Count == 0)
            {
                MessageBox.Show("请生成预览");
                return;
            }

            if (m_cfgBuilder == null)
            {
                return;
            }

            if (textVersion.Text == "" || cbProcode.Text == "")
            {
                MessageBox.Show("请输入!");
                return;
            }

            if (m_lstConfig.Count == 0)
            {
                MessageBox.Show("当前配置空!");
                return;
            }

            ClsDataSParamBuilder cfgObj = new ClsDataSParamBuilder(GDefine.DBNamePIM);
            if (cfgObj.constructByProcode(cbProcode.Text).Length != 0)
            {
                MessageBox.Show("该名称已配置");
                return;
            }

            string error = "";
            if (m_cfgBuilder.upload(cbProcode.Text, textVersion.Text, getFinalyConfig(), ref error))
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void AddTestItem(List<clsTestItem> Tlst, clsTestItem Alst)
        {
            if (Tlst.Count == 0)
            {
                Tlst.Add(Alst);
            }
            else
            {
                for (int i = 0; i < Tlst.Count; i++)
                {
                    if (Tlst[i].sPortA_RCU == Alst.sPortA_RCU && Tlst[i].sPortB_RCU == Alst.sPortB_RCU && Tlst[i].sTestName == Alst.sTestName && Tlst[i].sTestPortType == Alst.sTestPortType)
                    {
                        break;
                    }
                    else if (i == Tlst.Count - 1)
                    {
                        Tlst.Add(Alst);
                    }
                }
            }

        }

        private List<clsTestItem> getFinalyConfig()
        {
            List<clsTestItem> lstConfig = new List<clsTestItem>();
            for (int i = 0; i < dvgConfig.Rows.Count; i++)
            {
                //if ((bool)dvgConfig.Rows[i].Cells[0].Value)
                //{
                lstConfig.Add(m_lstConfig[i]);
                //}
            }
            return lstConfig;
        }


        private void btnAdd_TD_Click(object sender, EventArgs e)
        {
            if (cbRCUType.SelectedIndex != 0)
            {
                MessageBox.Show("TD天线只允许手动换度!");
                return;
            }
            textRcuIndex_TD.Text = textRcuIndex_TD.Text.Replace(" ", "");
            if (textRcuIndex_TD.Text == "")
            {
                MessageBox.Show("不允许空和空格，请重新输入!");
                return;
            }
            string[] sAngle = textAngle_TD.Text.Split(',');
            if (sAngle == null || sAngle.Length == 0)
            {
                MessageBox.Show("角度设置错误(数量不得小于1)!");
                return;
            }
            string[] sMarker = textMarker.Text.Split(',');
            if (sMarker == null || sMarker.Length == 0)
            {
                MessageBox.Show("标记点设置错误(数量不得小于1)!");
                return;
            }
            foreach (string str in sMarker)
            {
                if (str == "")
                {
                    MessageBox.Show("标记点设置错误(不能含空值)!");
                    return;
                }
            }

            TDParams param = new TDParams();
            m_tdparams.lstPortCount.Add(textConut_TD.Text);
            m_tdparams.lstBand.Add(textTestBand_TD.Text);
            m_tdparams.lstRCU.Add(textRcuIndex_TD.Text);
            m_tdparams.lstAngle.Add(textAngle_TD.Text);
            m_tdparams.lstMarker.Add(textMarker.Text);
            m_tdparams.lstcheckres.Add(checkresult_TD.Checked);
            m_tdparams.lstretspeed.Add(retspeed_TD.SelectedIndex);

            TDSpecParam p = new TDSpecParam();
            p.bEnable_Vswr = checkVSWRSpec_TD.Checked;
            p.sVswr = tbVSWRSpec_TD.Text;

            p.bEnable_MagSpec = checkAmSpec_TD.Checked;
            p.sMagSpec = tbMagSpec_TD.Text;

            p.bEnable_AmSpec = checkAmSpec_TD.Checked;
            p.sAmSpec = tbAmSpec_TD.Text;

            p.bEnable_PhaseSpec = checkPhaseSpec_TD.Checked;
            p.sPhaseSpec = tbPhaseSpec_TD.Text;

            p.bEnble_PhaseOffestSpec = checkPhaseOffestSpec_TD.Checked;
            p.sPhaseOffestSpec = tbPhaseOffestSpec_TD.Text;

            p.bEnable_CoIsoSpec = checkCoISOSpec_TD.Checked;
            p.sCoIsoSpec = tbCoISOSpec_TD.Text;

            p.bEnable_DeIsoSpec = checkDeISOSpec_TD.Checked;
            p.sDeIsoSpec = tbDeISOSpec_TD.Text;

            p.bEnable_IsoSpec = checkIsoSpec_TD.Checked;
            p.sIsoSpec = tbIsoSpec_TD.Text;

            m_tdparams.lstParam.Add(p);

            string rcu = textRcuIndex_TD.Text.Trim();
            string band = textTestBand_TD.Text.Trim();
            string agl = textAngle_TD.Text.Trim();
            string mark = textMarker.Text.Trim();
            if (!m_dicPort.ContainsKey(rcu))
            {
                m_dicPort.Add(rcu, new TestPort());
            }
            if (m_dicPort.ContainsKey(rcu))
            {
                if (!m_dicPort[rcu].dicBand.ContainsKey(band))
                {
                    m_dicPort[rcu].dicBand.Add(band, new TestBand());
                    m_dicPort[rcu].dicBand[band].band = band;
                    m_dicPort[rcu].dicBand[band].mark = mark;
                }
                TestAgl tg = new TestAgl();
                tg.agl = agl;
                tg.param = p;
                if (!m_dicPort[rcu].dicBand[band].dicAgl.ContainsKey(agl))
                {
                    m_dicPort[rcu].dicBand[band].dicAgl.Add(agl, tg);
                    string info = textTestBand_TD.Text + "/" + textRcuIndex_TD.Text + "/" + textAngle_TD.Text + "/" + textMarker.Text + "/" +
                         tbVSWRSpec_TD.Text + "/" + tbMagSpec_TD.Text + "/" + tbAmSpec_TD.Text + "/" + tbPhaseSpec_TD.Text + "/" + tbCoISOSpec_TD.Text + "/" + tbDeISOSpec_TD.Text + "/" + tbIsoSpec_TD.Text;
                    listAntennaPort.Items.Add(info);
                }
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (m_dtConfig != null)
                m_dtConfig.Clear();
            if (m_lstConfig != null)
                m_lstConfig.Clear();

            AntennaType antennatype;
            if (rbNormalAntenna.Checked)
            {
                antennatype = AntennaType.NUMAL;
            }
            else if (rbTdAntenna.Checked)
            {
                antennatype = AntennaType.TD;
            }
            else
            {
                antennatype = AntennaType.MIX;
            }
            if (m_dicPort.Count < 2)
            {
                this.Invoke(new ThreadStart(delegate { cbTestMethod.SelectedIndex = (int)AntennaTestMethod.Together; }));
            }
            AntennaTestMethod TestMethod;
            if (cbTestMethod.SelectedIndex == (int)AntennaTestMethod.Separate)
            {
                TestMethod = AntennaTestMethod.Separate;
            }
            else
            {
                TestMethod = AntennaTestMethod.Together;
            }

            string[] TestType = new string[4] { "驻波", "驻波", "隔离", "隔离" };
            string[] PortType = new string[4] { "S11", "S22", "S12", "S21" };
            string[] FormatType = new string[5] { "DISP_SWR", "DISP_SWR", "DISP_LOGM", "DISP_LOGM", "DISP_PHAS" };
            string[] TestPort = new string[9] { "P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "CAL" };

            if (m_dicPort.Count > 1)
            {
                List<string> lstkey = new List<string>();
                foreach (var obj in m_dicPort)
                {
                    lstkey.Add(obj.Key);
                    float minband = float.MaxValue, maxband = float.MinValue;
                    List<string> agls = new List<string>();
                    List<string> bands = new List<string>();
                    string marks = "";
                    foreach (var band in m_dicPort[obj.Key].dicBand)
                    {
                        bands.Add(band.Value.band);
                        marks += band.Value.mark + ",";
                        float b1 = float.Parse(band.Value.band.Split('-')[0]);
                        float b2 = float.Parse(band.Value.band.Split('-')[1]);
                        minband = minband > b1 ? b1 : minband;
                        minband = minband > b2 ? b2 : minband;
                        maxband = maxband < b1 ? b1 : maxband;
                        maxband = maxband < b2 ? b2 : maxband;
                        foreach (var agl in band.Value.dicAgl)
                        {
                            if (agls.IndexOf(agl.Key) < 0)
                                agls.Add(agl.Key);
                        }
                    }
                    foreach (var obj2 in m_dicPort)
                    {
                        if (lstkey.IndexOf(obj2.Key) > -1) continue;
                        lstkey.Add(obj2.Key);
                        List<string> agls2 = new List<string>();
                        List<string> bands2 = new List<string>();
                        foreach (var band in m_dicPort[obj2.Key].dicBand)
                        {
                            bands2.Add(band.Value.band);
                            marks += band.Value.mark + ",";
                            float b1 = float.Parse(band.Value.band.Split('-')[0]);
                            float b2 = float.Parse(band.Value.band.Split('-')[1]);
                            minband = minband > b1 ? b1 : minband;
                            minband = minband > b2 ? b2 : minband;
                            maxband = maxband < b1 ? b1 : maxband;
                            maxband = maxband < b2 ? b2 : maxband;
                            foreach (var agl in band.Value.dicAgl)
                            {
                                if (agls2.IndexOf(agl.Key) < 0)
                                    agls2.Add(agl.Key);
                            }
                        }
                        marks = marks.Substring(0, marks.Length - 1);
                        if (agls.Count != agls2.Count) { MessageBox.Show("两组端口角度个数不相等,无法生成带间隔离！"); return; }
                        for (int i = 0; i < agls.Count; i++)
                        {
                            if (!obj.Value.dicBand[bands[0]].dicAgl[agls[i]].param.bEnable_IsoSpec) continue;
                            for (int p = 1; p < TestPort.Length; p++)
                            {
                                var aConfig = new clsTestItem();
                                aConfig.m_arrDbHead = null;
                                aConfig.m_arrExcelHead = null;
                                aConfig.m_arrRCUHead = null;
                                aConfig.sIndex = i.ToString(); // 序号
                                aConfig.iTestIndex = 0; //接线序号
                                aConfig.sTestItem = "带间隔离";
                                string freq = minband + "-" + maxband;//带间隔离频率组合
                                aConfig.sTestName = obj.Key + "-" + obj2.Key + "/" + freq + "/" + agls[i] + "-" + agls2[i] + "/P" + p + "-P" + p + "/" + aConfig.sTestItem; // 参数名称
                                aConfig.sSpec = obj.Value.dicBand[bands[0]].dicAgl[agls[i]].param.sIsoSpec; // "客户要求"
                                aConfig.sFactSpec = aConfig.sSpec; // 内控
                                aConfig.sInstrCtrl = ""; // 仪表控制列
                                aConfig.sFreq = freq; // 频率
                                aConfig.sBand = textOffsetBand.Text; // 校准频段
                                aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80
                                aConfig.sTestFlag = "True";
                                aConfig.sPortA = "1"; // 1端口连接
                                aConfig.sPortB = "2"; // 2端口连接
                                aConfig.sTestPortType = PortType[3]; // 测试类型//S11,S21
                                aConfig.sFormatType = FormatType[3]; // 格式化类型//DISP_LOGM
                                aConfig.sReport = "定点值"; // 报表内容              
                                aConfig.sPortA_Angle = agls[i];//角度
                                aConfig.sPortB_Angle = agls2[i];
                                aConfig.sPortA_RCU = obj.Key + "-" + TestPort[p - 1];//端口名称
                                aConfig.sPortB_RCU = obj2.Key + "-" + TestPort[p - 1]; ;
                                aConfig.sTestFunction = "Single";
                                aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();
                                //aConfig.sCheckResult =false;
                                //aConfig.sRetSpeed = 1;
                                aConfig.sMarkers = marks;//带间隔离mark组合
                                aConfig.sAntennaType = (int)antennatype;
                                aConfig.sTestMethod = (int)TestMethod;
                                aConfig.sTestType = (int)AntennaTestType.InterbandIsolation;

                                AddTestItem(m_lstConfig, aConfig);
                            }
                        }

                    }
                }
            }

            foreach (var obj in m_dicPort)
            {
                foreach (var band in obj.Value.dicBand)
                {
                    int i = -1;
                    foreach (var agl in band.Value.dicAgl)
                    {
                        i++;
                        for (int p = 0; p < TestPort.Length; p++)
                        {
                            if (!obj.Value.dicBand[band.Key].dicAgl[agl.Key].param.bEnable_Vswr) break;
                            var aConfig = new clsTestItem();
                            aConfig.m_arrDbHead = null;
                            aConfig.m_arrExcelHead = null;
                            aConfig.m_arrRCUHead = null;
                            aConfig.sIndex = i.ToString(); // 序号  //测试会重组一次  可不填
                            aConfig.iTestIndex = 0; //接线序号
                            aConfig.sTestItem = "驻波";
                            aConfig.sTestName = obj.Key + "/" + band.Value.band + "/" + agl.Value.agl + "/" + TestPort[p] + "-" + TestPort[TestPort.Length - 1] + "/" + aConfig.sTestItem; // 参数名称
                            aConfig.sSpec = agl.Value.param.sVswr; // "客户要求"
                            aConfig.sFactSpec = aConfig.sSpec; // 内控
                            aConfig.sInstrCtrl = ""; // 仪表控制列
                            aConfig.sFreq = band.Value.band; // 频率
                            aConfig.sBand = textOffsetBand.Text; // 校准频段
                            aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80  // 角度索引 测试用
                            aConfig.sTestFlag = "True";
                            aConfig.sPortA = (p + 1).ToString(); // 1端口连接
                            aConfig.sPortB = TestPort.Length.ToString(); // 2端口连接
                            aConfig.sTestPortType = PortType[0]; // 测试类型//S11,S21
                            aConfig.sFormatType = FormatType[0]; // 格式化类型//DISP_LOGM
                            aConfig.sReport = "定点值"; // 报表内容              
                            aConfig.sPortA_Angle = agl.Value.agl;//角度
                            aConfig.sPortB_Angle = agl.Value.agl;
                            aConfig.sPortA_RCU = obj.Key + "-" + TestPort[p];//端口名称
                            aConfig.sPortB_RCU = obj.Key + "-" + TestPort[TestPort.Length - 1]; ;
                            aConfig.sTestFunction = "Single";
                            aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();
                            //aConfig.sCheckResult =false;
                            //aConfig.sRetSpeed = 1;
                            aConfig.sMarkers = band.Value.mark;
                            aConfig.sAntennaType = (int)antennatype;
                            aConfig.sTestMethod = (int)TestMethod;
                            aConfig.sTestType = (int)AntennaTestType.AmplitudePhase;

                            if (p == TestPort.Length - 1)
                            {
                                aConfig.sPortA = "1";
                                aConfig.sTestPortType = PortType[1]; // 测试类型//S11,S21
                                aConfig.sFormatType = FormatType[1]; // 格式化类型//DISP_LOGM
                            }
                            AddTestItem(m_lstConfig, aConfig);
                        }

                        string[] marks = band.Value.mark.Split(',');
                        for (int j = 0; j < marks.Length; j++)
                        {
                            for (int p = 0; p < TestPort.Length; p++)
                            {
                                if (!obj.Value.dicBand[band.Key].dicAgl[agl.Key].param.bEnable_MagSpec || !obj.Value.dicBand[band.Key].dicAgl[agl.Key].param.bEnable_AmSpec) break;
                                var aConfig = new clsTestItem();
                                aConfig.m_arrDbHead = null;
                                aConfig.m_arrExcelHead = null;
                                aConfig.m_arrRCUHead = null;
                                aConfig.sIndex = i.ToString(); // 序号
                                aConfig.iTestIndex = 0; //接线序号
                                aConfig.sTestItem = "耦合";
                                aConfig.sTestName = obj.Key + "/" + band.Value.band + "/" + agl.Value.agl + "/" + TestPort[p] + "-" + TestPort[TestPort.Length - 1] + "/" + marks[j] + "/" + aConfig.sTestItem; // 参数名称
                                aConfig.sSpec = agl.Value.param.sMagSpec; // "客户要求"
                                aConfig.sFactSpec = aConfig.sSpec; // 内控
                                aConfig.sInstrCtrl = ""; // 仪表控制列
                                aConfig.sFreq = band.Value.band; // 频率
                                aConfig.sBand = textOffsetBand.Text; // 校准频段
                                aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80
                                aConfig.sTestFlag = "True";
                                aConfig.sPortA = (p + 1).ToString(); // 1端口连接
                                aConfig.sPortB = TestPort.Length.ToString(); // 2端口连接
                                aConfig.sTestPortType = PortType[3]; // 测试类型//S11,S21
                                aConfig.sFormatType = FormatType[3]; // 格式化类型//DISP_LOGM
                                aConfig.sReport = "定点值"; // 报表内容              
                                aConfig.sPortA_Angle = agl.Value.agl;//角度
                                aConfig.sPortB_Angle = agl.Value.agl;
                                aConfig.sPortA_RCU = obj.Key + "-" + TestPort[p];//端口名称
                                aConfig.sPortB_RCU = obj.Key + "-" + TestPort[TestPort.Length - 1]; ;
                                aConfig.sTestFunction = "Single";
                                aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();
                                //aConfig.sCheckResult =false;
                                //aConfig.sRetSpeed = 1;
                                aConfig.sMarkers = marks[j];
                                aConfig.sAntennaType = (int)antennatype;
                                aConfig.sTestMethod = (int)TestMethod;
                                aConfig.sTestType = (int)AntennaTestType.AmplitudePhase;
                                if (p == TestPort.Length - 1)
                                {
                                    aConfig.sTestItem = "幅度一致性";
                                    aConfig.sTestName = obj.Key + "/" + band.Value.band + "/" + agl.Value.agl + "/" + TestPort[0] + "-" + TestPort[TestPort.Length - 2] + "/" + marks[j] + "/" + aConfig.sTestItem; // 参数名称
                                    aConfig.sPortA_RCU = obj.Key + "-" + TestPort[0];//端口名称
                                    aConfig.sPortB_RCU = obj.Key + "-" + TestPort[TestPort.Length - 2];
                                    aConfig.sSpec = agl.Value.param.sAmSpec; // "客户要求"
                                    aConfig.sFactSpec = aConfig.sSpec; // 内控
                                    aConfig.sPortA = "-1"; // 1端口连接
                                    aConfig.sPortB = "-1"; // 2端口连接
                                    aConfig.sReport = "最大值-最小值"; // 报表内容     
                                }

                                AddTestItem(m_lstConfig, aConfig);
                            }
                        }

                        for (int j = 0; j < marks.Length; j++)
                        {
                            for (int p = 0; p < TestPort.Length; p++)
                            {
                                if (!obj.Value.dicBand[band.Key].dicAgl[agl.Key].param.bEnable_PhaseSpec || !obj.Value.dicBand[band.Key].dicAgl[agl.Key].param.bEnble_PhaseOffestSpec) break;
                                var aConfig = new clsTestItem();
                                aConfig.m_arrDbHead = null;
                                aConfig.m_arrExcelHead = null;
                                aConfig.m_arrRCUHead = null;
                                aConfig.sIndex = i.ToString(); // 序号
                                aConfig.iTestIndex = 0; //接线序号
                                aConfig.sTestItem = "相位";
                                aConfig.sTestName = obj.Key + "/" + band.Value.band + "/" + agl.Value.agl + "/" + TestPort[p] + "-" + TestPort[TestPort.Length - 1] + "/" + marks[j] + "/" + aConfig.sTestItem; // 参数名称
                                aConfig.sSpec = agl.Value.param.sPhaseSpec; // "客户要求"
                                aConfig.sFactSpec = aConfig.sSpec; // 内控
                                aConfig.sInstrCtrl = ""; // 仪表控制列
                                aConfig.sFreq = band.Value.band; // 频率
                                aConfig.sBand = textOffsetBand.Text; // 校准频段
                                aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80
                                aConfig.sTestFlag = "True";
                                aConfig.sPortA = (p + 1).ToString(); // 1端口连接
                                aConfig.sPortB = TestPort.Length.ToString(); // 2端口连接
                                aConfig.sTestPortType = PortType[2]; // 测试类型//S11,S21
                                aConfig.sFormatType = FormatType[4]; // 格式化类型//DISP_LOGM
                                aConfig.sReport = "定点值"; // 报表内容              
                                aConfig.sPortA_Angle = agl.Value.agl;//角度
                                aConfig.sPortB_Angle = agl.Value.agl;
                                aConfig.sPortA_RCU = obj.Key + "-" + TestPort[p];//端口名称
                                aConfig.sPortB_RCU = obj.Key + "-" + TestPort[TestPort.Length - 1]; ;
                                aConfig.sTestFunction = "Single";
                                aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();
                                //aConfig.sCheckResult =false;
                                //aConfig.sRetSpeed = 1;
                                aConfig.sMarkers = marks[j];
                                aConfig.sAntennaType = (int)antennatype;
                                aConfig.sTestMethod = (int)TestMethod;
                                aConfig.sTestType = (int)AntennaTestType.AmplitudePhase;
                                if (p == TestPort.Length - 1)
                                {
                                    aConfig.sTestItem = "相位一致性";
                                    aConfig.sTestName = obj.Key + "/" + band.Value.band + "/" + agl.Value.agl + "/" + TestPort[0] + "-" + TestPort[TestPort.Length - 2] + "/" + marks[j] + "/" + aConfig.sTestItem; // 参数名称
                                    aConfig.sPortA_RCU = obj.Key + "-" + TestPort[0];//端口名称
                                    aConfig.sPortB_RCU = obj.Key + "-" + TestPort[TestPort.Length - 2];
                                    aConfig.sSpec = agl.Value.param.sPhaseOffestSpec; // "客户要求"
                                    aConfig.sFactSpec = aConfig.sSpec; // 内控
                                    aConfig.sPortA = "-1"; // 1端口连接
                                    aConfig.sPortB = "-1"; // 2端口连接
                                    aConfig.sReport = "最大值-最小值"; // 报表内容     
                                }

                                AddTestItem(m_lstConfig, aConfig);
                            }
                        }

                        #region  同极化隔离
                        int count = (TestPort.Length - 1) / 2;//端口总数9   (9-1)/2
                        int group = (TestPort.Length - 1) / count;//端口总数9/4  组数
                        for (int k = 0; k < group; k++)
                        {//四个口一组
                            if (!agl.Value.param.bEnable_CoIsoSpec) break;
                            for (int m = 1; m < count + 1; m++)
                            {
                                for (int n = m + 1; n <= count; n++)
                                {
                                    int p1 = k * count + m;
                                    int p2 = k * count + n;
                                    var aConfig = new clsTestItem();
                                    aConfig.m_arrDbHead = null;
                                    aConfig.m_arrExcelHead = null;
                                    aConfig.m_arrRCUHead = null;
                                    aConfig.sIndex = i.ToString(); // 序号
                                    aConfig.iTestIndex = 0; //接线序号
                                    aConfig.sTestItem = "同极化隔离";
                                    aConfig.sTestName = obj.Key + "/" + band.Value.band + "/" + agl.Value.agl + "/" + TestPort[p1 - 1] + "-" + TestPort[p2 - 1] + "/" + aConfig.sTestItem; // 参数名称
                                    aConfig.sSpec = agl.Value.param.sCoIsoSpec; // "客户要求"
                                    aConfig.sFactSpec = aConfig.sSpec; // 内控
                                    aConfig.sInstrCtrl = ""; // 仪表控制列
                                    aConfig.sFreq = band.Value.band; // 频率
                                    aConfig.sBand = textOffsetBand.Text; // 校准频段
                                    aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80
                                    aConfig.sTestFlag = "True";
                                    aConfig.sPortA = p1.ToString(); // 1端口连接
                                    aConfig.sPortB = p2.ToString(); // 2端口连接
                                    aConfig.sTestPortType = m_tdparams.csnPortType[3]; // 测试类型//S11,S21
                                    aConfig.sFormatType = m_tdparams.csnFormatType[3]; // 格式化类型//DISP_LOGM
                                    aConfig.sReport = "定点值"; // 报表内容              
                                    aConfig.sPortA_Angle = agl.Value.agl;//角度
                                    aConfig.sPortB_Angle = agl.Value.agl;
                                    aConfig.sPortA_RCU = obj.Key + "-" + TestPort[p1 - 1];//端口名称
                                    aConfig.sPortB_RCU = obj.Key + "-" + TestPort[p2 - 1]; ;
                                    aConfig.sTestFunction = "Single";
                                    aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();
                                    //aConfig.sCheckResult =false;
                                    //aConfig.sRetSpeed = 1;
                                    aConfig.sMarkers = band.Value.mark;
                                    aConfig.sAntennaType = (int)antennatype;
                                    aConfig.sTestMethod = (int)TestMethod;
                                    aConfig.sTestType = (int)AntennaTestType.AmplitudePhase;

                                    AddTestItem(m_lstConfig, aConfig);
                                }
                            }
                        }
                        #endregion
                        #region  异极化隔离
                        for (int k = 1; k <= count; k++)
                        {
                            if (!agl.Value.param.bEnable_DeIsoSpec) break;
                            for (int m = 5; m <= TestPort.Length - 1; m++)
                            {
                                var aConfig = new clsTestItem();
                                aConfig.m_arrDbHead = null;
                                aConfig.m_arrExcelHead = null;
                                aConfig.m_arrRCUHead = null;
                                aConfig.sIndex = i.ToString(); // 序号
                                aConfig.iTestIndex = 0; //接线序号
                                aConfig.sTestItem = "异极化隔离";
                                aConfig.sTestName = obj.Key + "/" + band.Value.band + "/" + agl.Value.agl + "/" + TestPort[k - 1] + "-" + TestPort[m - 1] + "/" + aConfig.sTestItem; // 参数名称
                                aConfig.sSpec = agl.Value.param.sDeIsoSpec; // "客户要求"
                                aConfig.sFactSpec = aConfig.sSpec; // 内控                            
                                aConfig.sInstrCtrl = ""; // 仪表控制列
                                aConfig.sFreq = band.Value.band; // 频率
                                aConfig.sBand = textOffsetBand.Text; // 校准频段
                                aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80
                                aConfig.sTestFlag = "True";
                                aConfig.sPortA = k.ToString(); // 1端口连接
                                aConfig.sPortB = m.ToString(); // 2端口连接
                                aConfig.sTestPortType = PortType[3]; // 测试类型//S11,S21
                                aConfig.sFormatType = FormatType[3]; // 格式化类型//DISP_LOGM
                                aConfig.sReport = "定点值"; // 报表内容              
                                aConfig.sPortA_Angle = agl.Value.agl;//角度
                                aConfig.sPortB_Angle = agl.Value.agl;
                                aConfig.sPortA_RCU = obj.Key + "-" + TestPort[k - 1];//端口名称
                                aConfig.sPortB_RCU = obj.Key + "-" + TestPort[m - 1];
                                aConfig.sTestFunction = "Single";
                                aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();
                                //aConfig.sCheckResult =false;
                                //aConfig.sRetSpeed = 1;
                                aConfig.sMarkers = band.Value.mark;
                                aConfig.sAntennaType = (int)antennatype;
                                aConfig.sTestMethod = (int)TestMethod;
                                aConfig.sTestType = (int)AntennaTestType.AmplitudePhase;

                                AddTestItem(m_lstConfig, aConfig);
                            }
                        }
                        #endregion
                    }
                }
            }

            #region 驻波, 带内隔离
            //for (int i = 0; i < angles.Length; i++)
            //{//角度
            //    for (int j = 0; j < sBand.Length; j++)
            //    {//频段
            //        #region 驻波
            //        for (int port = 1; port <= Convert.ToInt32(m_tdparams.lstPortCount[j]); port++)
            //        {//端口总数9
            //            if (!m_tdparams.lstParam[j].bEnable_Vswr) continue;
            //            var aConfig = new clsTestItem();
            //            aConfig.m_arrDbHead = null;
            //            aConfig.m_arrExcelHead = null;
            //            aConfig.m_arrRCUHead = null;
            //            aConfig.sIndex = j.ToString(); // 序号
            //            aConfig.iTestIndex = 0; //接线序号
            //            aConfig.sTestItem = "驻波";

            //            aConfig.sTestName = m_tdparams.lstPortCount[j] + "/" + m_tdparams.lstBand[j] + "-" + m_tdparams.lstRCU[j] + "/" + angles[i] + "-" + "P" + port + "/" + aConfig.sTestItem; // 参数名称                        
            //            aConfig.sSpec = m_tdparams.lstParam[j].sVswr; // "客户要求"
            //            aConfig.sFactSpec = m_tdparams.lstParam[j].sVswr; // 内控
            //            aConfig.sInstrCtrl = ""; // 仪表控制列
            //            aConfig.sFreq = m_tdparams.lstBand[j]; // 频率
            //            aConfig.sBand = textOffsetBand.Text; // 校准频段
            //            aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80

            //            aConfig.sTestFlag = "True";
            //            aConfig.sPortA = port.ToString(); // 1端口连接
            //            aConfig.sPortB = "9"; // 2端口连接

            //            aConfig.sTestPortType = m_tdparams.csnPortType[0]; // 测试类型//S11,S21
            //            aConfig.sFormatType = m_tdparams.csnFormatType[0]; // 格式化类型//DISP_LOGM
            //            aConfig.sReport = "定点值"; // 报表内容              

            //            aConfig.sPortA_Angle = angles[i];//角度
            //            aConfig.sPortB_Angle = angles[i];

            //            aConfig.sPortA_RCU = m_tdparams.lstRCU[j] + "-P" + port;//端口名称
            //            aConfig.sPortB_RCU = m_tdparams.lstRCU[j] + "-CAL";

            //            if (port == Convert.ToInt32(m_tdparams.lstPortCount[j]))
            //            {
            //                aConfig.sTestName = m_tdparams.lstPortCount[j] + "/" + m_tdparams.lstBand[j] + "-" + m_tdparams.lstRCU[j] + "/" + angles[i] + "-" + "CAL" + "/" + aConfig.sTestItem; // 参数名称
            //                aConfig.sTestPortType = m_tdparams.csnPortType[1]; // 测试类型//S11,S21
            //                aConfig.sFormatType = m_tdparams.csnFormatType[1]; // 格式化类型//DISP_LOGM
            //                aConfig.sPortA_RCU = m_tdparams.lstRCU[j] + "-CAL";//端口名称
            //                aConfig.sPortB_RCU = m_tdparams.lstRCU[j] + "-CAL";
            //            }

            //            aConfig.sTestFunction = "Single";
            //            aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();

            //            aConfig.sCheckResult = m_tdparams.lstcheckres[j];
            //            aConfig.sRetSpeed = m_tdparams.lstretspeed[j];

            //            aConfig.sMarkers = m_tdparams.lstMarker[j];
            //            aConfig.sAntennaType = (int)antennatype;
            //            aConfig.sTestMethod = (int)TestMethod;
            //            aConfig.sTestType = (int)AntennaTestType.AmplitudePhase;

            //            AddTestItem(m_lstConfig, aConfig);
            //        }
            //        #endregion

            //        #region 耦合/幅度一致性，相位/相位一致性
            //        for (int point = 0; point < m_tdparams.lstMarker[j].Split(',').Length; point++)
            //        {//频点                          
            //            var aConfig = new clsTestItem();
            //            #region 耦合  幅度一致性
            //            for (int port = 1; port <= Convert.ToInt32(m_tdparams.lstPortCount[j]); port++)
            //            {//端口总数9    
            //                if (!m_tdparams.lstParam[j].bEnable_MagSpec || !m_tdparams.lstParam[j].bEnable_AmSpec) continue;
            //                aConfig = new clsTestItem();
            //                aConfig.m_arrDbHead = null;
            //                aConfig.m_arrExcelHead = null;
            //                aConfig.m_arrRCUHead = null;
            //                aConfig.sIndex = j.ToString(); // 序号
            //                aConfig.iTestIndex = 0; //接线序号

            //                aConfig.sMarkers = m_tdparams.lstMarker[j].Split(',')[point];
            //                aConfig.sAntennaType = (int)antennatype;
            //                aConfig.sTestMethod = (int)TestMethod;
            //                aConfig.sTestType = (int)AntennaTestType.AmplitudePhase;


            //                aConfig.sTestItem = "耦合";

            //                aConfig.sTestName = m_tdparams.lstPortCount[j] + "/" + m_tdparams.lstBand[j] + "-" + m_tdparams.lstRCU[j] + "/" + angles[i] + "-" + "P" + port + "/" + aConfig.sMarkers + "/" + aConfig.sTestItem; // 参数名称

            //                aConfig.sSpec = m_tdparams.lstParam[j].sMagSpec; // "客户要求"
            //                aConfig.sFactSpec = m_tdparams.lstParam[j].sMagSpec; // 内控
            //                aConfig.sInstrCtrl = ""; // 仪表控制列
            //                aConfig.sFreq = m_tdparams.lstBand[j]; // 频率
            //                aConfig.sBand = textOffsetBand.Text; // 校准频段
            //                aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80

            //                aConfig.sTestFlag = "True";
            //                aConfig.sPortA = port.ToString(); // 1端口连接
            //                aConfig.sPortB = "9"; // 2端口连接

            //                aConfig.sTestPortType = m_tdparams.csnPortType[3]; // 测试类型//S11,S21
            //                aConfig.sFormatType = m_tdparams.csnFormatType[3]; // 格式化类型//DISP_LOGM
            //                aConfig.sReport = "定点值"; // 报表内容              

            //                aConfig.sPortA_Angle = angles[i];//角度
            //                aConfig.sPortB_Angle = angles[i];

            //                aConfig.sPortA_RCU = m_tdparams.lstRCU[j] + "-P" + port;//端口名称
            //                aConfig.sPortB_RCU = m_tdparams.lstRCU[j] + "-CAL";

            //                if (port == Convert.ToInt32(m_tdparams.lstPortCount[j]))
            //                {
            //                    aConfig.sTestItem = "幅度一致性";
            //                    aConfig.sTestName = m_tdparams.lstPortCount[j] + "/" + m_tdparams.lstBand[j] + "-" + m_tdparams.lstRCU[j] + "/" + angles[i] + "-" + "P1/8" + "/" + aConfig.sMarkers + "/" + aConfig.sTestItem; // 参数名称
            //                    aConfig.sPortA_RCU = m_tdparams.lstRCU[j] + "-P1";//端口名称
            //                    aConfig.sPortB_RCU = m_tdparams.lstRCU[j] + "-P8";
            //                    aConfig.sSpec = m_tdparams.lstParam[j].sAmSpec; // "客户要求"
            //                    aConfig.sFactSpec = m_tdparams.lstParam[j].sAmSpec; // 内控
            //                    aConfig.sPortA = "-1"; // 1端口连接
            //                    aConfig.sPortB = "-1"; // 2端口连接
            //                    aConfig.sReport = "最大值-最小值"; // 报表内容     
            //                }

            //                aConfig.sTestFunction = "Single";
            //                aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();

            //                aConfig.sCheckResult = m_tdparams.lstcheckres[j];
            //                aConfig.sRetSpeed = m_tdparams.lstretspeed[j];

            //                AddTestItem(m_lstConfig, aConfig);
            //            }
            //            #endregion

            //            #region 相位  相位一致性
            //            for (int port = 1; port <= Convert.ToInt32(m_tdparams.lstPortCount[j]); port++)
            //            {//端口总数9
            //                if (!m_tdparams.lstParam[j].bEnable_PhaseSpec || !m_tdparams.lstParam[j].bEnble_PhaseOffestSpec) continue;
            //                aConfig = new clsTestItem();
            //                aConfig.m_arrDbHead = null;
            //                aConfig.m_arrExcelHead = null;
            //                aConfig.m_arrRCUHead = null;
            //                aConfig.sIndex = j.ToString(); // 序号
            //                aConfig.iTestIndex = 0; //接线序号

            //                aConfig.sMarkers = m_tdparams.lstMarker[j].Split(',')[point];
            //                aConfig.sAntennaType = (int)antennatype;
            //                aConfig.sTestMethod = (int)TestMethod;
            //                aConfig.sTestType = (int)AntennaTestType.AmplitudePhase;

            //                aConfig.sSpec = m_tdparams.lstParam[j].sPhaseSpec; // "客户要求"
            //                aConfig.sFactSpec = m_tdparams.lstParam[j].sPhaseSpec; // 内控
            //                aConfig.sInstrCtrl = ""; // 仪表控制列
            //                aConfig.sFreq = m_tdparams.lstBand[j]; // 频率
            //                aConfig.sBand = textOffsetBand.Text; // 校准频段
            //                aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80

            //                aConfig.sTestFlag = "True";
            //                aConfig.sPortA = port.ToString(); // 1端口连接
            //                aConfig.sPortB = "9"; // 2端口连接

            //                aConfig.sTestPortType = m_tdparams.csnPortType[2]; // 测试类型//S11,S21
            //                aConfig.sFormatType = m_tdparams.csnFormatType[4]; // 格式化类型//DISP_LOGM
            //                aConfig.sReport = "定点值"; // 报表内容              

            //                aConfig.sPortA_Angle = angles[i];//角度
            //                aConfig.sPortB_Angle = angles[i];

            //                aConfig.sTestItem = "相位";
            //                aConfig.sTestName = m_tdparams.lstPortCount[j] + "/" + m_tdparams.lstBand[j] + "-" + m_tdparams.lstRCU[j] + "/" + angles[i] + "-" + "P" + port + "/" + aConfig.sMarkers + "/" + aConfig.sTestItem; // 参数名称

            //                aConfig.sPortA_RCU = m_tdparams.lstRCU[j] + "-P" + port;//端口名称
            //                aConfig.sPortB_RCU = m_tdparams.lstRCU[j] + "-CAL";

            //                if (port == Convert.ToInt32(m_tdparams.lstPortCount[j]))
            //                {
            //                    aConfig.sTestItem = "相位一致性";
            //                    aConfig.sTestName = m_tdparams.lstPortCount[j] + "/" + m_tdparams.lstBand[j] + "-" + m_tdparams.lstRCU[j] + "/" + angles[i] + "-" + "P1/8" + "/" + aConfig.sMarkers + "/" + aConfig.sTestItem; // 参数名称
            //                    aConfig.sPortA_RCU = m_tdparams.lstRCU[j] + "-P1";//端口名称
            //                    aConfig.sPortB_RCU = m_tdparams.lstRCU[j] + "-P8";
            //                    aConfig.sSpec = m_tdparams.lstParam[j].sPhaseOffestSpec; // "客户要求"
            //                    aConfig.sFactSpec = m_tdparams.lstParam[j].sPhaseOffestSpec; // 内控
            //                    aConfig.sPortA = "-1"; // 1端口连接
            //                    aConfig.sPortB = "-1"; // 2端口连接
            //                    aConfig.sReport = "最大值-最小值"; // 报表内容     
            //                }

            //                aConfig.sTestFunction = "Single";
            //                aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();

            //                aConfig.sCheckResult = m_tdparams.lstcheckres[j];
            //                aConfig.sRetSpeed = m_tdparams.lstretspeed[j];

            //                AddTestItem(m_lstConfig, aConfig);
            //            }
            //            #endregion
            //        }
            //        #endregion

            //        #region  同极化隔离

            //        int count = (Convert.ToInt32(m_tdparams.lstPortCount[j]) - 1) / 2;//端口总数9   (9-1)/2
            //        int group = (Convert.ToInt32(m_tdparams.lstPortCount[j]) - 1) / count;//端口总数9/4  组数
            //        for (int k = 0; k < group; k++)
            //        {//四个口一组
            //            if (!m_tdparams.lstParam[j].bEnable_CoIsoSpec) continue;
            //            for (int m = 1; m < count + 1; m++)
            //            {
            //                for (int n = m + 1; n <= count; n++)
            //                {
            //                    int p1 = k * count + m;
            //                    int p2 = k * count + n;
            //                    var aConfig = new clsTestItem();
            //                    aConfig.m_arrDbHead = null;
            //                    aConfig.m_arrExcelHead = null;
            //                    aConfig.m_arrRCUHead = null;
            //                    aConfig.sIndex = j.ToString(); // 序号
            //                    aConfig.iTestIndex = 0; //接线序号
            //                    aConfig.sTestItem = "同极化隔离";
            //                    aConfig.sTestName = m_tdparams.lstPortCount[j] + "/" + m_tdparams.lstBand[j] + "-" + m_tdparams.lstRCU[j] + "/" + angles[i] + "-" + "P" + p1 + p2 + "/" + aConfig.sTestItem; // 参数名称

            //                    aConfig.sSpec = m_tdparams.lstParam[j].sCoIsoSpec; // "客户要求"
            //                    aConfig.sFactSpec = m_tdparams.lstParam[j].sCoIsoSpec; // 内控
            //                    aConfig.sInstrCtrl = ""; // 仪表控制列
            //                    aConfig.sFreq = m_tdparams.lstBand[j]; // 频率
            //                    aConfig.sBand = textOffsetBand.Text; // 校准频段
            //                    aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80

            //                    aConfig.sTestFlag = "True";
            //                    aConfig.sPortA = p1.ToString(); // 1端口连接
            //                    aConfig.sPortB = p2.ToString(); // 2端口连接

            //                    aConfig.sTestPortType = m_tdparams.csnPortType[3]; // 测试类型//S11,S21
            //                    aConfig.sFormatType = m_tdparams.csnFormatType[3]; // 格式化类型//DISP_LOGM
            //                    aConfig.sReport = "定点值"; // 报表内容              

            //                    aConfig.sPortA_Angle = angles[i];//角度
            //                    aConfig.sPortB_Angle = angles[i];

            //                    aConfig.sPortA_RCU = m_tdparams.lstRCU[j] + "-P" + p1;//端口名称
            //                    aConfig.sPortB_RCU = m_tdparams.lstRCU[j] + "-P" + p2;

            //                    aConfig.sTestFunction = "Single";
            //                    aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();

            //                    aConfig.sCheckResult = m_tdparams.lstcheckres[j];
            //                    aConfig.sRetSpeed = m_tdparams.lstretspeed[j];

            //                    aConfig.sMarkers = m_tdparams.lstMarker[j];
            //                    aConfig.sAntennaType = (int)antennatype;
            //                    aConfig.sTestMethod = (int)TestMethod;
            //                    aConfig.sTestType = (int)AntennaTestType.AmplitudePhase;

            //                    AddTestItem(m_lstConfig, aConfig);
            //                }
            //            }
            //        }
            //        #endregion

            //        #region  异极化隔离
            //        for (int k = 1; k <= count; k++)
            //        {
            //            if (!m_tdparams.lstParam[j].bEnable_DeIsoSpec) continue;
            //            for (int m = 5; m <= (Convert.ToInt32(m_tdparams.lstPortCount[j]) - 1); m++)
            //            {
            //                var aConfig = new clsTestItem();
            //                aConfig.m_arrDbHead = null;
            //                aConfig.m_arrExcelHead = null;
            //                aConfig.m_arrRCUHead = null;
            //                aConfig.sIndex = j.ToString(); // 序号
            //                aConfig.iTestIndex = 0; //接线序号
            //                aConfig.sTestItem = "异极化隔离";
            //                aConfig.sTestName = m_tdparams.lstPortCount[j] + "/" + m_tdparams.lstBand[j] + "-" + m_tdparams.lstRCU[j] + "/" + angles[i] + "-" + "P" + k + m + "/" + aConfig.sTestItem; // 参数名称

            //                aConfig.sSpec = m_tdparams.lstParam[j].sDeIsoSpec; // "客户要求"
            //                aConfig.sFactSpec = m_tdparams.lstParam[j].sDeIsoSpec; // 内控
            //                aConfig.sInstrCtrl = ""; // 仪表控制列
            //                aConfig.sFreq = m_tdparams.lstBand[j]; // 频率
            //                aConfig.sBand = textOffsetBand.Text; // 校准频段
            //                aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80

            //                aConfig.sTestFlag = "True";
            //                aConfig.sPortA = k.ToString(); // 1端口连接
            //                aConfig.sPortB = m.ToString(); // 2端口连接

            //                aConfig.sTestPortType = m_tdparams.csnPortType[3]; // 测试类型//S11,S21
            //                aConfig.sFormatType = m_tdparams.csnFormatType[3]; // 格式化类型//DISP_LOGM
            //                aConfig.sReport = "定点值"; // 报表内容              

            //                aConfig.sPortA_Angle = angles[i];//角度
            //                aConfig.sPortB_Angle = angles[i];

            //                aConfig.sPortA_RCU = m_tdparams.lstRCU[j] + "-P" + k;//端口名称
            //                aConfig.sPortB_RCU = m_tdparams.lstRCU[j] + "-P" + m;

            //                aConfig.sTestFunction = "Single";
            //                aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();

            //                aConfig.sCheckResult = m_tdparams.lstcheckres[j];
            //                aConfig.sRetSpeed = m_tdparams.lstretspeed[j];

            //                aConfig.sMarkers = m_tdparams.lstMarker[j];
            //                aConfig.sAntennaType = (int)antennatype;
            //                aConfig.sTestMethod = (int)TestMethod;
            //                aConfig.sTestType = (int)AntennaTestType.AmplitudePhase;

            //                AddTestItem(m_lstConfig, aConfig);
            //            }
            //        }
            //        #endregion
            //    }

            //    for (int j = 0; j < m_tdparams.lstBand.Count; j++)
            //    {//频段
            //        #region 带间隔离
            //        if (m_tdparams.lstBand[j].Length >= 2)
            //        {//两组频段以上、两组端口以上才有带间隔离
            //            if (!m_tdparams.lstParam[j].bEnable_IsoSpec) continue;
            //            for (int nextj = j + 1; nextj < m_tdparams.lstBand.Count; nextj++)
            //            {
            //                for (int port = 1; port < Convert.ToInt32(m_tdparams.lstPortCount[j]); port++)
            //                {
            //                    var aConfig = new clsTestItem();
            //                    aConfig.m_arrDbHead = null;
            //                    aConfig.m_arrExcelHead = null;
            //                    aConfig.m_arrRCUHead = null;
            //                    aConfig.sIndex = j.ToString(); // 序号
            //                    aConfig.iTestIndex = 0; //接线序号
            //                    aConfig.sTestItem = "带间隔离";

            //                    string freq = m_tdparams.lstBand[j].Split('-')[0] +"-"+ m_tdparams.lstBand[nextj].Split('-')[1];//带间隔离频率组合

            //                    aConfig.sTestName = m_tdparams.lstPortCount[j] + "/" + m_tdparams.lstBand[j] + "-" + m_tdparams.lstRCU[j] + "/" + m_tdparams.lstBand[nextj] + "-" + m_tdparams.lstRCU[nextj] + "/" + angles[i] + "-" + "P" + port + port + "/" + aConfig.sTestItem; // 参数名称


            //                    aConfig.sSpec = m_tdparams.lstParam[j].sIsoSpec; // "客户要求"
            //                    aConfig.sFactSpec = m_tdparams.lstParam[j].sIsoSpec; // 内控
            //                    aConfig.sInstrCtrl = ""; // 仪表控制列
            //                    aConfig.sFreq = freq; // 频率
            //                    aConfig.sBand = textOffsetBand.Text; // 校准频段
            //                    aConfig.sRandom = (i + 1).ToString(); //测试次数 随机范围//1,2, 50~80

            //                    aConfig.sTestFlag = "True";
            //                    aConfig.sPortA = "1"; // 1端口连接
            //                    aConfig.sPortB = "2"; // 2端口连接

            //                    aConfig.sTestPortType = m_tdparams.csnPortType[3]; // 测试类型//S11,S21
            //                    aConfig.sFormatType = m_tdparams.csnFormatType[3]; // 格式化类型//DISP_LOGM
            //                    aConfig.sReport = "定点值"; // 报表内容              

            //                    aConfig.sPortA_Angle = angles[i];//角度
            //                    aConfig.sPortB_Angle = angles[i];

            //                    aConfig.sPortA_RCU = m_tdparams.lstRCU[j] + "-P" + port;//端口名称
            //                    aConfig.sPortB_RCU = m_tdparams.lstRCU[nextj] + "-P" + port;

            //                    aConfig.sTestFunction = "Single";
            //                    aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();

            //                    aConfig.sCheckResult = m_tdparams.lstcheckres[j];
            //                    aConfig.sRetSpeed = m_tdparams.lstretspeed[j];

            //                    aConfig.sMarkers = m_tdparams.lstMarker[j] + "," + m_tdparams.lstMarker[nextj];//带间隔离mark组合
            //                    aConfig.sAntennaType = (int)antennatype;
            //                    aConfig.sTestMethod = (int)TestMethod;
            //                    aConfig.sTestType = (int)AntennaTestType.InterbandIsolation;

            //                    AddTestItem(m_lstConfig, aConfig);
            //                }
            //            }
            //        }
            //        #endregion
            //    }
            //}
            #endregion



            m_dtConfig = ClsDataSParamBuilder.toTable(m_lstConfig);

            dvgConfig.DataSource = m_dtConfig;
            if (dvgConfig.Columns[0].HeaderText != "")
            {
                var colCheck = new DataGridViewCheckBoxColumn();
                dvgConfig.Columns.Insert(0, colCheck);
            }
            //for (int i = 0; i < dvgConfig.Rows.Count; i++)
            //{
            //    dvgConfig.Rows[i].Cells[0].Value = true;
            //}
        }

        void NumalTestConfigBuild()
        {
            if (m_dtConfig != null)
                m_dtConfig.Clear();
            if (m_lstConfig != null)
                m_lstConfig.Clear();
            int SwitchConnectIndex = 1;
            Dictionary<string, string> dicAllRcu = new Dictionary<string, string>();
            for (int i = 0; i < lstRCU.Count; i++)
            {
                if (!dicAllRcu.ContainsKey(lstRCU[i]))
                {
                    dicAllRcu.Add(lstRCU[i], string.Format("{0},{1}", SwitchConnectIndex, SwitchConnectIndex + 1));
                    SwitchConnectIndex += 2;
                }
            }
            #region 驻波, 带内隔离
            for (int j = 0; j < lstBand.Count; j++)
            {//驻波，带内端口隔离
                for (int typeNum = 0; typeNum < csnPortType.Length; typeNum++)
                {
                    var aConfig = new clsTestItem();
                    aConfig.m_arrDbHead = null;
                    aConfig.m_arrExcelHead = null;
                    aConfig.m_arrRCUHead = null;

                    int test_index = 0;// j * 2 / m_switch_port_count;
                    aConfig.sIndex = test_index.ToString(); // 序号
                    aConfig.iTestIndex = 0; //接线序号

                    aConfig.sTestItem = csnTestType[typeNum];
                    aConfig.sTestName = lstBand[j] + "-" + lstRCU[j] + "/" +
                                        lstAngle_min[j] + "-" + lstAngle_max[j] + "/" +
                                        csnTestType[typeNum]; // 参数名称

                    string sSPec = csnTestType[typeNum] == "驻波" ? lstParam[j].sVswr : csnTestType[typeNum] == "隔离" ? lstParam[j].sIsoSpec_Port : csnTestType[typeNum] == "插损" ? lstParam[j].sInsertlossSpec : lstParam[j].sMagSpec;
                    aConfig.sSpec = sSPec; // "客户要求"
                    aConfig.sFactSpec = sSPec; // 内控

                    aConfig.sInstrCtrl = ""; // 仪表控制列
                    aConfig.sFreq = lstBand[j]; // 频率
                    aConfig.sBand = textOffsetBand.Text; // 校准频段
                    aConfig.sRandom = "1"; //测试次数 随机范围//1,2, 50~80

                    if (csnTestType[typeNum] == "驻波" && !lstParam[j].bEnable_Vswr)
                    {
                        continue;
                    }
                    else if (csnTestType[typeNum] == "隔离" && !lstParam[j].bEnable_IsoSpec_Port)
                    {
                        continue;
                    }
                    else if (csnTestType[typeNum] == "插损" && !lstParam[j].bEnable_InsertlossSpec)
                    {
                        continue;
                    }
                    else if (csnTestType[typeNum] == "耦合" && !lstParam[j].bEnable_MagSpec)
                    {
                        continue;
                    }

                    aConfig.sTestFlag = "True";


                    aConfig.sPortA = dicAllRcu[lstRCU[j]].Split(',')[0];// (2 * j + 1).ToString(); // (2 * j % m_switch_port_count + 1).ToString(); // 1端口连接
                    aConfig.sPortB = dicAllRcu[lstRCU[j]].Split(',')[1];// (2 * j + 1 + 1).ToString(); // (2 * j % m_switch_port_count + 1 + 1).ToString(); // 2端口连接

                    aConfig.sTestPortType = csnPortType[typeNum]; // 测试类型//S11,S21
                    aConfig.sFormatType = csnFormatType[typeNum]; // 格式化类型//DISP_LOGM
                    if (csnTestType[typeNum] == "插损")
                        aConfig.sReport = "最小值的绝对值"; // 报表内容    
                    else
                        aConfig.sReport = "最大值的绝对值"; // 报表内容              

                    aConfig.sPortA_Angle = lstAngle_min[j] + "-" + lstAngle_max[j];
                    aConfig.sPortB_Angle = lstAngle_min[j] + "-" + lstAngle_max[j];

                    aConfig.sPortA_RCU = lstRCU[j] + "+45度";
                    aConfig.sPortB_RCU = lstRCU[j] + "-45度";
                    if (lstRcuTip[j] != "&")
                    {
                        aConfig.sPortA_Tip = lstRcuTip[j].Split('&')[0];
                        aConfig.sPortB_Tip = lstRcuTip[j].Split('&')[1];
                    }
                    else
                    {
                        aConfig.sPortA_Tip = aConfig.sPortA_RCU;
                        aConfig.sPortB_Tip = aConfig.sPortB_RCU;
                    }



                    aConfig.sTestFunction = "Continue";

                    aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();

                    aConfig.sCheckResult = lstcheckres[j];
                    aConfig.sRetSpeed = lstretspeed[j];
                    
                    aConfig.sPoint = Convert.ToInt32(textPoint.Text.Trim());
                    aConfig.sFileVer = textFileVersion.Text.Trim();

                    AddTestItem(m_lstConfig, aConfig);
                }
            }
            #endregion

            #region  同频异频带间隔离
            List<string> lstRCU_X = new List<string>(lstRCU);
            List<string> lstBand_X = new List<string>(lstBand);
            /*--------------------TDA0100473天线特殊处理-------------------------------------*/
            //存在10347项（123端口+10224极化隔离）；特定删减为极化隔离用端口统一大频段测试，共235项（123+112）,存在问题：测试指标与端口数据不对应（取了前几项），由于473极化隔离所有指标要求一致，可忽略此问题
            Dictionary<string, string> dicAllRcuBand = new Dictionary<string, string>();
            if (check473.Checked)
            {
                for (int i = 0; i < lstRCU.Count; i++)
                {
                    if (!dicAllRcuBand.ContainsKey(lstRCU[i]))
                        dicAllRcuBand.Add(lstRCU[i], lstBand[i]);
                    else
                    {
                        if (dicAllRcuBand[lstRCU[i]] == lstBand[i]) continue;
                        string[] sband = dicAllRcuBand[lstRCU[i]].Split('-');
                        string[] sbandN = lstBand[i].Split('-');
                        if (sband.Length != 2 || sbandN.Length != 2)
                        {
                            sband = dicAllRcuBand[lstRCU[i]].Split('～');
                            sbandN = lstBand[i].Split('-');
                        }
                        if (sband.Length != 2 || sbandN.Length != 2) { MessageBox.Show("频段设置错误", "Err"); return; }
                        float f1 = float.Parse(sband[0]);
                        float f2 = float.Parse(sband[1]);
                        float f1N = float.Parse(sbandN[0]);
                        float f2N = float.Parse(sbandN[1]);
                        if (f1 > f2 || f1N > f2N) { MessageBox.Show("频段设置错误", "Err"); return; }
                        if (f1 == -1 || f2 == -1 || f1N == -1 || f2N == -1)
                        {
                            MessageBox.Show("频段设置错误", "Err");
                            return;
                        }
                        if (f1 > f1N) f1 = f1N;
                        if (f2 < f2N) f2 = f2N;
                        dicAllRcuBand[lstRCU[i]] = f1 + "-" + f2;
                    }
                }
                lstBand_X.Clear();
                lstRCU_X.Clear();
                foreach (var va in dicAllRcuBand)
                {
                    lstRCU_X.Add(va.Key);
                    lstBand_X.Add(va.Value);
                }
            }
            /*--------------------TDA0100473天线特殊处理-------------------------------------*/

            string[] sPortType = new string[2] { "S12", "S21" };
            string[] sAntennaPort = new string[2] { "+45度", "-45度" };
            for (int typeNum = 0; typeNum < sPortType.Length; typeNum++)
                for (int angleNum = 0; angleNum < 2; angleNum++)
                    for (int portA_Num = 0; portA_Num < lstBand_X.Count; portA_Num++)
                    {//端口A
                        for (int portA_AntNum = 0; portA_AntNum < sAntennaPort.Length; portA_AntNum++)
                        {//端口A天线端口
                            for (int portB_Num = portA_Num + 1; portB_Num < lstRCU_X.Count; portB_Num++)
                            {//端口B
                                for (int portB_AntNum = 0; portB_AntNum < sAntennaPort.Length; portB_AntNum++)
                                {//端口B天线端口
                                    if (lstBand_X[portA_Num] == lstBand_X[portB_Num] && sPortType[typeNum] == "S21")
                                    {//同频带间隔离的S21跳过
                                        continue;
                                    }
                                    if (lstRCU_X[portA_Num] == lstRCU_X[portB_Num])
                                    {/*同一端口，极化是相同的，不存在极化隔离度；隔离度是两个端口才存在，就像矩阵开关，两个端口间隔离度越高，性能越好，越不受影响
                                      * --江焰波2018-7-2*/
                                        continue;
                                    }


                                    var aConfig = new clsTestItem();
                                    aConfig.m_arrDbHead = null;
                                    aConfig.m_arrExcelHead = null;
                                    aConfig.m_arrRCUHead = null;

                                    aConfig.sIndex = ""; // 序号
                                    aConfig.iTestIndex = 0; //接线序号

                                    string sSpec = "";
                                    if (lstBand_X[portA_Num] == lstBand_X[portB_Num])
                                    {
                                        aConfig.sTestItem = "同频带间隔离";
                                        if (sPortType[typeNum] == "S21")
                                            sSpec = lstParam[portB_Num].sIsoSpec_SameFreq;
                                        else
                                            sSpec = lstParam[portA_Num].sIsoSpec_SameFreq;

                                        if (!lstParam[portA_Num].bEnable_IsoSpec_SameFreq)
                                            continue;
                                        //    aConfig.sTestFlag = "True"; //true X
                                        //else
                                        //    aConfig.sTestFlag = "False"; //true X
                                    }
                                    else
                                    {
                                        aConfig.sTestItem = "异频带间隔离";
                                        if (sPortType[typeNum] == "S21")
                                            sSpec = lstParam[portB_Num].sIsoSpec_Freq;
                                        else
                                            sSpec = lstParam[portA_Num].sIsoSpec_Freq;

                                        if (!lstParam[portA_Num].bEnable_IsoSpec_Freq)
                                            continue;
                                        //    aConfig.sTestFlag = "True"; //true X
                                        //else
                                        //    aConfig.sTestFlag = "False"; //true X
                                    }

                                    aConfig.sSpec = sSpec; // "客户要求"
                                    aConfig.sFactSpec = sSpec; // 内控

                                    aConfig.sInstrCtrl = ""; // 仪表控制列
                                    aConfig.sFreq = typeNum == 0 ? lstBand_X[portA_Num] : lstBand_X[portB_Num]; // 频率
                                    aConfig.sBand = textOffsetBand.Text; // 校准频段
                                    aConfig.sRandom = "1"; //(换线)测试次数 随机范围//1,2, 50~80
                                    aConfig.sTestFlag = "True"; //true X
                                    aConfig.sIndex = "0";

                                    aConfig.sPortA = (2 * portA_Num + portA_AntNum + 1).ToString(); // 1端口连接
                                    aConfig.sPortB = (2 * portB_Num + portB_AntNum + 1).ToString(); // 2端口连接

                                    aConfig.sTestPortType = sPortType[typeNum]; // 测试类型//S11,S21
                                    aConfig.sFormatType = "DISP_LOGM"; // 格式化类型//DISP_LOGM
                                    aConfig.sReport = "最大值的绝对值"; // 报表内容              

                                    string angleA = angleNum == 0 ? lstAngle_min[portA_Num] : lstAngle_max[portA_Num];
                                    string angleB = angleNum == 0 ? lstAngle_min[portB_Num] : lstAngle_max[portB_Num];
                                    aConfig.sPortA_Angle = angleA;
                                    aConfig.sPortB_Angle = angleB;
                                    aConfig.sPortA_RCU = lstRCU_X[portA_Num] + sAntennaPort[portA_AntNum];
                                    aConfig.sPortB_RCU = lstRCU_X[portB_Num] + sAntennaPort[portB_AntNum];

                                    if (sAntennaPort[portA_AntNum] == sAntennaPort[0])
                                        aConfig.sPortA = dicAllRcu[lstRCU_X[portA_Num]].Split(',')[0];
                                    else
                                        aConfig.sPortA = dicAllRcu[lstRCU_X[portA_Num]].Split(',')[1];
                                    if (sAntennaPort[portB_AntNum] == sAntennaPort[0])
                                        aConfig.sPortB = dicAllRcu[lstRCU_X[portB_Num]].Split(',')[0];
                                    else
                                        aConfig.sPortB = dicAllRcu[lstRCU_X[portB_Num]].Split(',')[1];


                                    if (lstRcuTip[portA_Num] != "&")
                                    {
                                        aConfig.sPortA_Tip = lstRcuTip[portA_Num].Split('&')[portA_AntNum];
                                    }
                                    else
                                    {
                                        aConfig.sPortA_Tip = aConfig.sPortA_RCU;
                                    }
                                    if (lstRcuTip[portB_Num] != "&")
                                    {
                                        aConfig.sPortB_Tip = lstRcuTip[portB_Num].Split('&')[portB_AntNum];
                                    }
                                    else
                                    {
                                        aConfig.sPortB_Tip = aConfig.sPortB_RCU;
                                    }


                                    aConfig.sTestFunction = "Single";
                                    aConfig.sRCUFlag = cbRCUType.SelectedIndex.ToString();
                                    //aConfig.sRCUFlag = !checkManualRCU.Checked ? "1" : "0";

                                    aConfig.sPoint = Convert.ToInt32(textPoint.Text.Trim());
                                    aConfig.sFileVer = textFileVersion.Text.Trim();

                                    aConfig.sTestName = lstBand_X[portA_Num] + "-" + lstRCU_X[portA_Num] + "/" +
                                                        lstBand_X[portB_Num] + "-" + lstRCU_X[portB_Num] + "/" +
                                                        aConfig.sPortA_Angle + "/" + aConfig.sPortB_Angle + "/" +
                                                        aConfig.sTestItem; // 参数名称

                                    m_lstConfig.Add(aConfig);
                                }
                            }
                        }
                    }

            #endregion

            m_dtConfig = ClsDataSParamBuilder.toTable(m_lstConfig);

            dvgConfig.DataSource = m_dtConfig;
            if (dvgConfig.Columns[0].HeaderText != "")
            {
                var colCheck = new DataGridViewCheckBoxColumn();
                dvgConfig.Columns.Insert(0, colCheck);
            }
            //for (int i = 0; i < dvgConfig.Rows.Count; i++)
            //{
            //    dvgConfig.Rows[i].Cells[0].Value = true;
            //}

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }


        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
                rbNormalAntenna.Checked = true;
            else if (tabControl1.SelectedIndex == 1)
                rbTdAntenna.Checked = true;
        }
    }
}
